package com.bgds.sys.data.iXCom29d.gateway.impl;

import com.cisdi.data.common.utils.Dates;

import java.util.Date;
/**
 * @author
 */
public class BoBuilder {

	public static BaoXingVo dataAckSuccess(String msgKey, String sendDc, String receiveDc) {
		
		char[] bodyCharacters = new char[80];
		
		for (int i = 0; i < bodyCharacters.length; i++) {
			bodyCharacters[i] = BaoXingConsts.BLANK;
		}
		
		String body = new String(bodyCharacters);
		
		return data(msgKey, BaoXingConsts.FUNC_CODE_ANSWER, sendDc, receiveDc, body, 80);
	}
	
	public static BaoXingVo dataAckFail(String msgKey, String sendDc, String receiveDc, String errMsg) {
		return data(msgKey, BaoXingConsts.FUNC_CODE_ERROR, sendDc, receiveDc, errMsg, 80);
	}
	
	/**
	 * 
	 * @param msgKey
	 * @param funcCode
	 * @param sendDc
	 * @param receiveDc
	 * @param body
	 * @param fixBodySize 报文体固定长度，用于响应消息时控制域必须为80个字符的情况。大于0，截断或填充body；小于0，传入body即最终body。
	 * @return
	 */
	public static BaoXingVo data(String msgKey, Character funcCode, String sendDc, String receiveDc,  String body, int fixBodySize) {
		BaoXingVo vo = new BaoXingVo();
		Date date = new Date();
		vo.setMsgKey(msgKey);
		vo.setDate(Dates.format(date, "yyyyMMdd"));
		vo.setTime(Dates.format(date, "HHmmss"));
		vo.setSendDc(sendDc);
		vo.setReceiveDc(receiveDc);
		vo.setFuncCode(funcCode);
		
		String realbody = body;
		
		if(fixBodySize > 0) {
			if(body.length() > fixBodySize) {
				realbody = body.substring(0, fixBodySize);
			}else {
				char[] leftChar = new char[fixBodySize - body.length()];
			    for (int i = 0; i < leftChar.length; i++) {
					leftChar[i] = BaoXingConsts.BLANK;
				}
			    realbody = body + new String(leftChar);
			}
		}
		
		vo.setBody(realbody);
		
		return vo;
	}
}
